package com.lm.itextpdf.example;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfOutline;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

public class AddBookmarksExample {
    public static void main(String[] args) throws Exception {
        String dest = "bookmarked.pdf";
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdfDoc);

        PdfOutline root = pdfDoc.getOutlines(false);
        // 添加第一个章节的书签
        PdfOutline firstChapter = root.addOutline("Chapter 1");
        firstChapter.addDestination(PdfExplicitDestination.createFit(pdfDoc.getPage(1)));

        // 添加第二个章节的书签
        PdfOutline secondChapter = root.addOutline("Chapter 2");
        secondChapter.addDestination(PdfExplicitDestination.createFit(pdfDoc.getPage(2)));
        document.close();
        System.out.println("PDF with bookmarks created at: " + dest);
    }
}
